#include "qonsole.h"
#include <QDockWidget>

Qonsole::Qonsole(QWidget *parent)
    : QMainWindow(parent)
{
    m_Logw = new LogWindow("debug");
    m_Logw->setReadOnly(true);
    setCentralWidget(m_Logw);
    m_InputArea = new QLineEdit();
    QDockWidget* qdw = new QDockWidget("Type commands here");
    qdw->setWidget(m_InputArea);
    addDockWidget(Qt::BottomDockWidgetArea, qdw);
    connect(m_InputArea, &QLineEdit::returnPressed, this, &Qonsole::execute);

    m_Bash = new QProcess();
    m_Bash->setReadChannelMode(QProcess::MergedChannels);
    connect(m_Bash, &QProcess::readyReadStandardOutput, this, &Qonsole::showOutput);
    m_Bash->start("C:/Windows/system32/cmd.exe");
}

Qonsole::~Qonsole()
{

}

void Qonsole::execute()
{
    QString cmdStr = m_InputArea->text() + "\n";
    m_InputArea->setText("");
//    m_Logw->append(cmdStr);
    QByteArray bytes = cmdStr.toUtf8();
    m_Bash->write(bytes);
}

void Qonsole::showOutput()
{
    QByteArray bytes = m_Bash->readAllStandardOutput();
    QStringList lines = QString::fromLocal8Bit(bytes.data()).split("\n");
    foreach (QString line, lines) {
        m_Logw->append(line);
    }
}
